package com.xgry.bpm.api.base.socketuser.service.impl;

import com.xgry.bpm.api.base.socketuser.entity.SocketUser;
import com.xgry.bpm.api.base.socketuser.mapper.SocketUserMapper;
import com.xgry.bpm.api.base.socketuser.service.SocketUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.apache.http.HttpStatus;

import java.util.List;
import java.util.stream.Collectors;

/**
* <p>
    * SocketUser服务实现类
    * </p>
*
* @author adrian
* @since 2020-07-08
*/
@Service
public class SocketUserServiceImpl extends ServiceImpl<SocketUserMapper, SocketUser>implements SocketUserService {

    @Override
    public int deleteById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public SocketUser selectById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        SocketUser data=baseMapper.selectById(id);
        return data;
    }

    @Override
    public List<SocketUser>selectAll(SocketUser record){
        QueryWrapper<SocketUser>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        List<SocketUser>list=baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<SocketUser>selectPage(IPage<SocketUser> page , SocketUser record){
        // 查询条件
        QueryWrapper<SocketUser>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public IPage<SocketUser>getPage(IPage<SocketUser>page,QueryWrapper<SocketUser>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<SocketUser> findByfoUser(int foUser) {
        return baseMapper.selectList(new QueryWrapper<SocketUser>(){{
            this.eq("socketfoUser",foUser);
            this.eq("messageStutas",1);
        }});
    }

    @Override
    public int countMessage(Integer userId) {
        return baseMapper.selectList(new QueryWrapper<SocketUser>(){{
            this.eq("messageStutas",1);
            this.eq("socketUser",userId);
        }}).size();
    }

    @Override
    public List<SocketUser> findByUser(Integer userId) {
        return baseMapper.selectList(new QueryWrapper<SocketUser>(){{
            this.eq("socketUser",userId);
        }});
    }

    @Override
    public List<SocketUser> unreadMessage(Integer userId) {
        return baseMapper.selectList(new QueryWrapper<SocketUser>(){{
            this.eq("socketUser",userId);
            this.eq("messageStutas",1);
        }});
    }

    @Override
    public List<SocketUser> readMessage(Integer userId) {
        return baseMapper.selectList(new QueryWrapper<SocketUser>(){{
            this.eq("socketUser",userId);
            this.eq("messageStutas",2);
        }});
    }

    @Override
    public boolean deleteSocketUsers(List<SocketUser> list) {
        List<Integer> collect = list.stream().map(s -> s.getSocketId()).collect(Collectors.toList());
        baseMapper.deleteBatchIds(collect);
        return true;
    }

}